'use strict';

const mysql = require('mysql');

class MySQLPipeline {

    open_spider(spider) {
        const server = spider.options.ds;
        if (!server) {
            spider.log.error('You must set MySQL connect str first!')
            process.exit(1)
        }
        this.client = mysql.createConnection(server)
    }

    async process_item(item, spider, request) {
        const { log } = spider;
        const { target } = spider.options;
        const { index_name, id, file = {}, ...body } = item;
        const rowId = id ? id : file.basename || request.reqKey;

        if (target && rowId) {
            this.client.connect();
            try {
                this.client.query(`INSERT INTO ${target} SET ?`, {
                    id: spider.name + ":" + rowId,
                    _spider_name: spider.name,
                    _spider_time: new Date(),
                    _spider_uri: request.link,
                    ...file,
                    ...body
                }, function (error, results, fields) {
                    if (error) throw error;
                    log.debug(`${target} index ${rowId} ok`)
                })
            } catch (error) {
                console.log('insert error: ', error)
            }
            this.client.end();
        }
        return item;
    }

}

module.exports = MySQLPipeline;